.\"-
.\" SPDX-License-Identifier: BSD-3-Clause
.\"
.\" Copyright (c) 1983, 1991, 1993
.\"	The Regents of the University of California.  All rights reserved.
.\" Copyright (c) 2025 The FreeBSD Foundation
.\"
.\" Portions of this documentation were written by Olivier Certner
.\" <olce@FreeBSD.org> at Kumacom SARL under sponsorship from the FreeBSD
.\" Foundation.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\"    may be used to endorse or promote products derived from this software
.\"    without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.Dd October 9, 2025
.Dt INITGROUPS 3
.Os
.Sh NAME
.Nm initgroups
.Nd initialize supplementary groups as per the group database
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
.In unistd.h
.Ft int
.Fn initgroups "const char *name" "gid_t basegid"
.Sh DESCRIPTION
The
.Fn initgroups
function initializes the current process' supplementary groups as prescribed by
its arguments and the system's group database.
.Pp
It first uses the
.Fn getgrouplist
function to compute a list of groups containing the passed
.Fa basegid ,
which typically is the user's initial numerical group ID from the password
database, and the supplementary groups in the group database for the user named
.Fa name .
It then installs this list as the current process' supplementary groups using
.Fn setgroups .
.Sh RETURN VALUES
.Rv -std initgroups
.Sh ERRORS
The
.Fn initgroups
function may fail and set
.Va errno
to any of the errors specified for the
.Xr setgroups 2
system call, or to:
.Bl -tag -width Er
.It Bq Er ENOMEM
The
.Fn initgroups
function was unable to allocate temporary storage.
.El
.Sh SEE ALSO
.Xr setgroups 2 ,
.Xr getgrouplist 3
.Sh HISTORY
The
.Fn initgroups
function appeared in
.Bx 4.2 .
.Pp
The
.Fn initgroups
function changed semantics in
.Fx 15 ,
following that of
.Xr setgroups 2
in the same release.
Before that, it would also set the effective group ID to
.Fa basegid ,
and would not include the latter in the supplementary groups except before
.Fx 8 .
Its current behavior in these respects is known to be compatible with that of
the following systems up to the specified versions that are current at time of
this writing:
.Bl -dash -width "-" -compact
.It
Linux (up to 6.6) with the GNU libc (up to 2.42)
.It
.Nx 1.1 and greater (up to 10)
.It
.Ox (up to 7.7)
.It
Systems based on illumos (up to August 2025 sources)
.El
.Sh SECURITY CONSIDERATIONS
As
.Fa basegid
is typically the user's initial numerical group ID, to which the current
process' effective group ID is generally initialized, processes using functions
to change their effective group ID
.Pq via Xr setgid 2 or similar
or that are spawned from executables with the set-group-ID mode bit set will not
be able to relinquish the access rights deriving from being a member of
.Fa basegid ,
as these functions do not change the supplementary groups.
.Pp
This behavior is generally desirable in order to paper over the difference of
treatment between the effective group and supplementary ones in this situation,
as they are all in the end indiscriminately used in traditional UNIX
discretionary access checks.
It blends well with the practice of allocating each user its own private group,
as processes launched from a set-group-ID executable keep the same user and
consistently stay also in the same user's group.
Finally, it was also chosen for compatibility with other systems
.Po
see the
.Sx HISTORY
section
.Pc .
.Pp
This convention of including
.Fa basegid
in the supplementary groups is however only enforced by the
.Fn initgroups
function, and not by the
.Xr setgroups 2
system call, so applications expressly wanting to include in the supplementary
groups only those specified by the group database can themselves call
.Fn getgrouplist
and then
.Fn setgroups
on the result with the first element skipped
.Pq see Xr getgrouplist 3 .
